﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using kysec.RAL.EFToMySql;
using System.Data.Linq.SqlClient;

namespace kysec_MVC.Controllers
{
    public class ProductController : Controller
    {
        #region 后台操作显示
        /// <summary>
        /// 实现分页显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult InfoList(int? id, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("Products_{0}_{1}", id, type + DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_product> orders = (from c in db.tb_product
                                                                     where c.c_i_type == type
                                                                     orderby c.c_dt_sendTime descending
                                                                     select c).ToPagedList(id ?? 1, 10);

                    if (Request.IsAjaxRequest())
                        return PartialView("UCProductsList", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("Products_{0}_{1}", id, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_product>)o);
        }

        /// <summary>
        /// 实现查询数据，根据标题，模糊……
        /// </summary>
        public ActionResult Search(string title, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("DetailProduct_{0}", title));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_product> orders = (from c in db.tb_product
                                                                     where c.c_i_type == type && SqlMethods.Like(c.c_vc_name, title + "%")
                                                                     orderby c.c_dt_sendTime descending
                                                                     select c).ToPagedList(1, 10);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                        string.Format("DetailProduct_{0}", title),
                        orders,
                        1,
                        System.Web.Caching.CacheItemPriority.Normal);
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_product>)o);
        }

        public ActionResult AddProducts()
        {
            return View();
        }
        /// <summary>
        /// 添加产品基本信息
        /// </summary>
        [ValidateInput(false)]
        [HttpPost]
        public void AddProducts(int type,tb_product model)
        {
            string result = "0";

            result = kysec.BLL.Production.products.AddProduct(type, model);

            if (result.Equals("1"))
            {
                Response.Write("<script type='text/javascript'>alert('添加成功!')</script>");
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Product/InfoList?type=" + type);
            }            
        }

        /// <summary>
        /// 查询数据信息和修改数据信息
        /// </summary>
        public ActionResult UpdateProduct(string id, int type, bool isUpdate)
        {
            List<tb_product> list = kysec.BLL.Production.Search.getProducts(id);

            if (isUpdate)
                return View(list);
            else
                return PartialView("Search", list);
        }
        /// <summary>
        /// 修改产品信息
        /// </summary>
        [ValidateInput(false)]
        [HttpPost]
        public void UpdateProduct(string id, tb_product model,string type)
        {
            string result = "0";

            model.c_i_type = Convert.ToInt32(type);

            result = kysec.BLL.Production.products.UpdateProduct(id, model);

            if (result.Equals("1"))
            {
                Response.Write("<script type='text/javascript'>alert('修改成功!')</script>");
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Product/InfoList?type=" + type);
            }
        }

        /// <summary>
        /// 设置是否为样品演示的转换
        /// </summary>
        public void setDemo(string id, int isDemo,string type)
        {
            string result = "0";
            bool demoTrue = isDemo == 1 ? true : false;
            result = kysec.BLL.Production.products.setDemo(id, demoTrue);

            if (result.Equals("1"))
            {
                Response.Write("<script type='text/javascript'>alert('添加成功!')</script>");
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Product/InfoList?type=" + type);
            }
        }

        /// <summary>
        /// 添加样品演示
        /// </summary>
        public ActionResult AddDemo(string id)
        {
            List<tb_product> list = kysec.BLL.Production.Search.getProducts(id);
            return View(list);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddDemo(string id, tb_product model,string imgPath)
        {
            string result = "0";

            int type = Convert.ToInt32(model.c_i_type);           


            result = kysec.BLL.Production.products.AddDemo(id, model,imgPath);

            if (result.Equals("1"))
            {
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Product/InfoList?type=" + type);
               return Content("<script type='text/javascript'>alert('添加成功!')</script>");
               
            }
            else
            {
                return View();
            }

        }

        /// <summary>
        /// 删除和恢复此产品
        /// </summary>
        public void Delete(string id,string type)
        {
            string result = "0";

            result = kysec.BLL.Production.products.DeleteProduct(id);

            if (result.Equals("1"))
            {
                Response.Write("<script type='text/javascript'>alert('添加成功!')</script>");
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Product/InfoList?type=" + type);
            }
        }

        /// <summary>
        /// 删除此产品
        /// </summary>
        public string DeleteOne(string id)
        {
            string result = "0";

            result = kysec.BLL.Production.products.DeleteProduct(id);

            return result;
        }

        #endregion


        #region 前台页面显示
        public ActionResult Product_Demo()
        {
            List<kysec.BLL.Production.DemoData> new_list = kysec.BLL.Production.Search.getDemoProducts(0, true,20, 0).ToList();
            ViewData["new_Products"] = new_list;

            List<kysec.BLL.Production.DemoData> xilie_list = kysec.BLL.Production.Search.getDemoProducts(1,true, 20, 0).ToList();
            ViewData["xilie_Products"] = xilie_list;

            List<kysec.BLL.Production.DemoData> pList = new List<kysec.BLL.Production.DemoData>();
            pList = new_list.Concat(xilie_list).ToList();
            ViewData["Products"] = pList;
            return View();
        }
        public ActionResult Demo(string id)
        {
            
            List<kysec.BLL.Production.DemoDetail> list = kysec.BLL.Production.Search.getProductsForDemo(id);
            List<kysec.BLL.Production.DemoData> new_list = kysec.BLL.Production.Search.getDemoProducts(0, true, 20, 0).ToList();
            ViewData["new_Products"] = new_list;

            List<kysec.BLL.Production.DemoData> xilie_list = kysec.BLL.Production.Search.getDemoProducts(1, true, 20, 0).ToList();
            ViewData["xilie_Products"] = xilie_list;

            List<kysec.BLL.Production.DemoData> pList = new List<kysec.BLL.Production.DemoData>();
            pList = new_list.Concat(xilie_list).ToList();
            ViewData["Products"] = pList;
            return View(list);
        }
        /// <summary>
        /// 展示具体的产品
        /// </summary>
        /// <param name="id">产品ID，若为空就默认为最新的产品</param>
        /// <returns></returns>
        public ActionResult ProductShow(string id)
        {
            List<kysec.BLL.Production.DemoData> list = kysec.BLL.Production.Search.getProductsLeft(0, 20, 0).ToList();
            ViewData["newProduct"] = list;

            List<kysec.BLL.Production.DemoData> listStr = kysec.BLL.Production.Search.getProductsLeft(1, 20, 0).ToList();
            ViewData["XilieProduct"] = listStr;
            List<kysec.RAL.EFToMySql.tb_product> pList = new List<tb_product>();
            if (id == null)
            {
                pList = kysec.BLL.Production.Search.getNewsProducts();
            }
            else
            {
                pList = kysec.BLL.Production.Search.getProducts(id);
            }
            return View(pList);
        }

        /// <summary>
        /// 展示产品的初始页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductList()
        {
            List<kysec.BLL.Production.DemoData> list = kysec.BLL.Production.Search.getProductsLeft(0, 2, 0).ToList();
            ViewData["newProduct"] = list;

            List<kysec.BLL.Production.DemoData> listStr = kysec.BLL.Production.Search.getProductsLeft(1, 20, 0).ToList();
            ViewData["XilieProduct"] = listStr;
            return View();
        }
        #endregion

    }
}
